package com.yogee.topseller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.base.HttpFragment1;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.SearchActivity;
import com.yogee.topseller.fragment.product.model.bean.ProductListBean;
import com.yogee.topseller.fragment.product.model.bean.ProductTypeListBean;
import com.yogee.topseller.fragment.product.presenter.ProductPresenter;
import com.yogee.topseller.fragment.product.view.IproductView;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;
import com.yogee.topseller.utils.TabLayoutUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 产品列表
 *
 * @auathor ${XiaoJianghong}
 * @date 2017/6/20.
 */

public class ProductFragment extends HttpFragment1 implements IproductView {

    @BindView(R.id.tl_product_type1)
    TabLayout tlProductType1;
    @BindView(R.id.vp_product_list1)
    ViewPager vpProductList1;
    @BindView(R.id.tv_transparent)
    TextView tvTransparent;
    @BindView(R.id.iv_go_search)
    ImageView ivGoSearch;
    
    Unbinder unbinder;
    //Fragment的数据集合
    List<Fragment> fragmentList = new ArrayList<>();
    //标题数据集合
    List<String> titles = new ArrayList<>();
    HomeProductFragment fragment;
    ProductPresenter productPresenter;
    View rootView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_product, container, false);
        unbinder = ButterKnife.bind(this, rootView);

        setListener();
        return rootView;
    }

    @Override
    protected void initView() {
        LogUtils.e("homeinit","2222222222222222222");
        productPresenter = new ProductPresenter(this);
        productPresenter.loadProductTypeList();
    }

    private void setListener() {
        tlProductType1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置最后tab的透明度控件消失
                if (tab.getPosition() == fragmentList.size() - 1 || tab.getPosition() == fragmentList.size() - 2) {
                    tvTransparent.setVisibility(View.INVISIBLE);
                } else {
                    tvTransparent.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取产品类别回调
     */
    @Override
    public void loadProductTypeSuccess(ProductTypeListBean productTypeListBean) {
        //需要把“全部”自己加上
        Bundle bundle = null;
        if (null != productTypeListBean.getSegment() && productTypeListBean.getSegment().size()>0){
            for (int i = 0; i < (productTypeListBean.getSegment().size() + 1); i++) {
                //设置标题
                if (0 == i) {
//                    titles.add("全部" + "(" + productTypeListBean.getCount() + ")");
//                    titles.add("全部");
                } else {
                    titles.add(productTypeListBean.getSegment().get(i - 1).getName());
                    //设置内容
                    bundle = new Bundle();
                    bundle.putSerializable("titleBean", productTypeListBean.getSegment().get(i - 1));
                    fragment = new HomeProductFragment();
                    fragment.setArguments(bundle);
                    fragmentList.add(fragment);
                }

            }
            new TabLayoutUtil(getActivity().getSupportFragmentManager(), rootView.findViewById(R.id.product_list_layout), fragmentList, titles, 1);
        }
    }

    @Override
    public void loadProductSuccess(ProductListBean bean) {

    }

    @OnClick(R.id.iv_go_search)
    public void onViewClicked() {
        if(AppUtil.isExamined(getActivity())){
            startActivity(new Intent(getActivity(), SearchActivity.class));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("产品"); //统计页面("MainScreen"为页面名称，可自定义)
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("产品");
    }

}
